package com.ruoyi.member.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.member.config.MinioConfig;
import com.ruoyi.member.mapper.MemberMapper1;
import com.ruoyi.member.utils.MinioUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.member.domain.Member;
import com.ruoyi.member.service.IMemberService;
import org.springframework.web.multipart.MultipartFile;

/**
 * #商家信息Service业务层处理
 * 
 * @author ruoyi-bai
 * @date 2023-08-02
 */
@Service
public class MemberServiceImpl implements IMemberService
{
    @Autowired
    private MemberMapper1 memberMapper1;
    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public Map<String, Object> uploadFile(MultipartFile file) {
        boolean flag = false;
        if (file.isEmpty()) {
            throw new RuntimeException("文件不存在！");
        }
        // 判断存储桶是否存在
        if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
            minioUtil.makeBucket(minioConfig.getBucketName());
        }
        // 生成文件名
        String newFileName = minioUtil.getUUIDFileName(file.getOriginalFilename());
        try {
            // 上传文件
            flag = minioUtil.upload(file, newFileName, minioConfig.getBucketName());

        } catch (Exception e) {
            return null;
        }
//         判断是否上传成功，成功就返回url，不成功就返回null
        Map<String,Object> map = new HashMap<>();
        map.put("url",minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + newFileName);
        map.put("fileName",newFileName);
        map.put("code",200);
        String filePath=minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + newFileName;
        Thumbnails.of(filePath)
                .scale(0.25f);
        return map;
    }

//    @Override
//    public String uploadFile(MultipartFile file) {
//        boolean flag = false;
//        if (file.isEmpty()) {
//            throw new RuntimeException("文件不存在！");
//        }
//        // 判断存储桶是否存在
//        if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
//            minioUtil.makeBucket(minioConfig.getBucketName());
//        }
//        // 生成文件名
//        String newFileName = minioUtil.getUUIDFileName(file.getOriginalFilename());
//        try {
//            // 上传文件
//            flag = minioUtil.upload(file, newFileName, minioConfig.getBucketName());
//        } catch (Exception e) {
//            return null;
//        }
////         判断是否上传成功，成功就返回url，不成功就返回null
////        Map<String,Object> map = new HashMap<>();
////        map.put("url",minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + newFileName);
////        map.put("fileName",newFileName);
////        map.put("code",200);
//        return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + newFileName;
//    }

    /**
     * 查询#商家信息
     * 
     * @param id #商家信息主键
     * @return #商家信息
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper1.selectMemberById(id);
    }

    /**
     * 查询#商家信息列表
     * 
     * @param member #商家信息
     * @return #商家信息
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper1.selectMemberList(member);
    }

    /**
     * 新增#商家信息
     * 
     * @param member #商家信息
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        return memberMapper1.insertMember(member);
    }

    /**
     * 修改#商家信息
     * 
     * @param member #商家信息
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        return memberMapper1.updateMember(member);
    }

    /**
     * 批量删除#商家信息
     * 
     * @param ids 需要删除的#商家信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper1.deleteMemberByIds(ids);
    }

    /**
     * 删除#商家信息信息
     * 
     * @param id #商家信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper1.deleteMemberById(id);
    }
}
